#!/bin/sh
clear
ssh-keygen -t rsa -N '' -f $HOME/.ssh/id_rsa -q #生成公钥文件
ssh-keygen -R 192.168.31.95#删除公钥
read -p "请输入IP段(如192.168.1):" IPDuan
read -p "请输入远端的用户名:" remoteUser
iplist=$(nmap -p 22 $IPDuan.1-255 | grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}")
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for i in ${iplist}
do
expect <<EOF
set timeout 5
spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub ${remoteUser}@${i}
expect {
    "yes/no" {send "yes\r"; exp_continue}
    "password:" {send "123456\r"}
    }
expect eof
EOF
done   #将公钥发送给每一个IP
# ssh book@192.168.31.135
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++